package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.JsonResp;
import com.rongke.enums.EachTypeEnum;
import com.rongke.model.EachPicture;
import com.rongke.model.Goods;
import com.rongke.service.EachPictureService;
import com.rongke.service.GoodsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version : Ver 1.0
 * @EachPictureController
 * @Controller
 */
@RestController
@RequestMapping(value = "/api/eachPicture")
@Transactional
@CrossOrigin
public class EachPictureController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private EachPictureService eachPictureService;
    @Autowired
    private GoodsService goodsService;

    /**
     * @param eachPicture
     * @return 返回值JsonResp
     * @添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JsonResp addEachPicture(@RequestBody EachPicture eachPicture) {
        log.debug("添加");
        if (null != eachPicture.getGoodsId()) {
            Goods goods = goodsService.selectById(eachPicture.getGoodsId());
            if (null == goods)
                return JsonResp.toFail("该商品不存在");
        }
        if (eachPicture.getEachType() > 3) {
            EntityWrapper<EachPicture> ew = new EntityWrapper<>();
            ew.eq("each_type", eachPicture.getEachType());
            List<EachPicture> eachPictureList = eachPictureService.selectList(ew);
            if (eachPictureList.size() == 0) {
                eachPictureService.insert(eachPicture);
            } else {
                eachPicture.setId(eachPictureList.get(0).getId());
                eachPictureService.updateById(eachPicture);
            }
        } else {
            eachPictureService.insert(eachPicture);
        }
        return JsonResp.ok(eachPicture);
    }

    /**
     * @param eachPicture
     * @return 返回值JsonResp
     * @修改
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResp updateEachPicture(@RequestBody EachPicture eachPicture) {
        log.debug("修改");
        eachPictureService.updateById(eachPicture);
        return JsonResp.ok(eachPicture);
    }

    /**
     * @param id 轮播图id
     * @return 返回值JsonResp
     * @删除
     */
    @RequestMapping(value = "/doDelete", method = RequestMethod.GET)
    public JsonResp doDelete(Long id) {
        log.debug("轮播图删除");
        eachPictureService.deleteById(id);
        return JsonResp.ok("");
    }


    /**
     * @param type 轮播类型
     * @return 返回值JsonResp
     * @获取轮播列表
     */
    @RequestMapping(value = "/findEachPicList", method = RequestMethod.POST)
    public JsonResp findEachPicList(Integer type) {
        log.debug("获取轮播列表");
        EntityWrapper<EachPicture> ew = new EntityWrapper<>();
        ew.eq("each_type", type);
        List<EachPicture> eachPicture = eachPictureService.selectList(ew);
        return JsonResp.ok(eachPicture);
    }

    /**
     * @param id
     * @return 返回值JsonResp
     * @根据id查找
     */
    @RequestMapping(value = "/selectOne", method = RequestMethod.GET)
    public JsonResp selectEachPicture(Long id) {
        log.debug("查找");
        EachPicture eachPicture = eachPictureService.selectById(id);
        return JsonResp.ok(eachPicture);
    }


    /**
     * @return 返回值JsonResp
     * @获取主页轮播图列表
     */
    @RequestMapping(value = "/selectEachPictureList", method = RequestMethod.GET)
    public JsonResp selectEachPictureList() {
        log.debug("查找");
        EntityWrapper<EachPicture> ew = new EntityWrapper<>();
        ew.eq("each_type", EachTypeEnum.INDEX.getType());
        List<EachPicture> eachPictureList = eachPictureService.selectList(ew);
        return JsonResp.ok(eachPictureList);
    }


}
